<template>
  <div id="airports">
    <div class="card">
      <div class="card-body">
        <form class="row">
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="城市名"
              size="8"
              v-model.trim="searchName"
            />
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary"
              @click.stop="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary btn-sm ms-2"
              @click.stop="resetSearch()"
            >
              重置
            </button>

            <span class="ms-auto">
              <a
                href="javascript:void(0)"
                v-on:click="newCity()"
                class="btn btn-success btn-lg"
                >新建</a
              >
            </span>
          </div>
        </form>
      </div>
    </div>
    <table class="table table-striped table-hover table-sm small">
      <thead>
        <tr>
          <th>中文名</th>
          <th>英文名</th>
          <th>城市代码</th>
          <th>省份</th>
          <th>国家代码</th>
          <th>国家名称</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(info, index) in dataList" :key="info.id">
          <td>{{ info.cityName }}</td>
          <td>{{ info.cityEnglishName }}</td>
          <td>{{ info.cityCode }}</td>
          <td>{{ info.provinceName }}</td>
          <td>{{ info.countryCode }}</td>
          <td>{{ info.countryName }}</td>
          <td>
            <button class="btn btn-sm btn-warning" @click.stop="edit(index)">
              修改
            </button>
          </td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <!-- New Request Modal -->
    <div
      class="modal"
      id="modalPolicyManage"
      tabindex="-1"
      role="dialog"
      aria-labelledby="myModalLabel"
      aria-hidden="true"
    >
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title" id="myModalLabel">新增城市信息</h4>
            <button type="button" class="close" data-dismiss="modal">
              <span aria-hidden="true">&times;</span
              ><span class="sr-only">Close</span>
            </button>
          </div>
          <div class="modal-body">
            <form id="frmUser" role="form" class="form-horizontal">
              <div class="row">
                <label class="control-label col-4"> 中文名 </label>
                <div class="col-8">
                  <input type="text" class="form-control" v-model.trim="name" />
                </div>
              </div>
              <div class="row">
                <label class="control-label col-4"> 英文名 </label>
                <div class="col-8">
                  <input
                    type="text"
                    class="form-control"
                    v-model.trim="englishName"
                  />
                </div>
              </div>
              <div class="row">
                <label class="control-label col-4"> 城市代码 </label>
                <div class="col-8">
                  <input
                    type="text"
                    class="form-control"
                    v-model.trim="cityCode"
                  />
                </div>
              </div>
              <div class="row">
                <label class="control-label col-4"> 省份名称 </label>
                <div class="col-8">
                  <input
                    type="text"
                    class="form-control"
                    v-model.trim="provinceName"
                  />
                </div>
              </div>
              <div class="row">
                <label class="control-label col-4"> 国家代码 </label>
                <div class="col-8">
                  <input
                    type="text"
                    class="form-control"
                    v-model.trim="countryCode"
                  />
                </div>
              </div>
              <div class="row">
                <label class="control-label col-4"> 国家名 </label>
                <div class="col-8">
                  <input
                    type="text"
                    class="form-control"
                    v-model.trim="countryName"
                  />
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              取消
            </button>
            <button
              type="button"
              class="btn btn-primary"
              v-on:click.stop="saveCity()"
            >
              保存
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";
import { searchCities, saveCity } from "@/api/basic-data.js";
import MyPagination from "@/components/my-pagination.vue";

export default {
  components: {
    MyPagination,
  },
  data() {
    return {
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 50,
        pageTotal: 0,
      },
      searchName: "",

      id: 0,
      name: "",
      englishName: "",
      cityCode: "",
      provinceName: "",
      countryCode: "",
      countryName: "",

      dataList: [],
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        name: this.searchName,
      };

      searchCities(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    reset: function () {
      this.id = 0;
      this.name = "";
      this.englishName = "";
      this.cityCode = "";
      this.provinceName = "";
      this.countryCode = "";
      this.countryName = "";
    },
    newCity: function () {
      this.reset();

      $("#modalPolicyManage").modal();
    },
    edit: function (index) {
      const info = this.dataList[index];

      this.id = info.id;

      this.name = info.cityName;
      this.englishName = info.cityEnglishName;
      this.cityCode = info.cityCode;
      this.provinceName = info.provinceName;
      this.countryCode = info.countryCode;
      this.countryName = info.countryName;

      $("#modalPolicyManage").modal();
    },
    saveCity: function () {
      const params = {
        id: this.id,
        name: this.name,
        englishName: this.englishName,
        cityCode: this.cityCode,
        provinceName: this.provinceName,
        countryCode: this.countryCode,
        countryName: this.countryName,
      };

      saveCity(params, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg);
        } else {
          $("#modalPolicyManage").modal("hide");
          this.search();
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
    resetSearch: function () {
      this.searchName = "";
    },
  },
};
</script>
